package com.nondo.dean.product.controller.api.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.product.domain.ProductRelative;
import com.nondo.dean.product.model.result.RelativeResult;
import com.nondo.dean.product.service.ProductRelativeService;


/**
 * 产品关联管理
 * @author zlj
 * @date 2018年3月28日
 */
@RestController("apiAdminProductRelative")
@RequestMapping("/{v}/admin/product/relative")
public class ProductRelativeController {

    @Autowired
    private ProductRelativeService productRelativeService;

    /**
     * 查看产品关联详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<RelativeResult> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<RelativeResult> view = new View<RelativeResult>(20000, "获取数据成功");
        
        RelativeResult relativeBean = null;
        if (id != null) {
            relativeBean = productRelativeService.getProductByRelativeId(id);
        } else {
            relativeBean = new RelativeResult();
        }

        view.setData(relativeBean);
        return view;
    }
    
    /**
     * 添加产品关联
     * @param request
     * @param response
     * @param productRelative 产品关联信息 {@link ProductRelative}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<ProductRelative> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody ProductRelative productRelative, BindingResult result) {
        View<ProductRelative> view = new View<ProductRelative>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            ProductRelative productRelativeDb =
                this.productRelativeService.getRelativeByProductIdAndRelativeId(productRelative.getProductId(),
                    productRelative.getRelativeProductId());
            if (productRelativeDb != null) {
                return View.of500Error("产品已存在关联关系");
            }
            this.productRelativeService.addProductRelative(productRelative);
        }
        return view;
    }

    /**
     * 更新产品关联
     * @param request
     * @param response
     * @param productRelative 产品关联信息 {@link ProductRelative}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<ProductRelative> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody ProductRelative productRelative, BindingResult result) {
        View<ProductRelative> view = new View<ProductRelative>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            ProductRelative productRelativeDb = productRelativeService.selectProductRelative(productRelative.getId());
            ProductRelative productRelativeDb2 =
                this.productRelativeService.getRelativeByProductIdAndRelativeId(productRelative.getProductId(),
                    productRelative.getRelativeProductId());
            if (productRelativeDb.getId() != productRelativeDb2.getId()) {
                return View.of500Error("产品已存在关联关系");
            }
            BeanUtils.copy(productRelative, productRelativeDb);
            this.productRelativeService.updateProductRelative(productRelativeDb);
        }

        return view;
    }

    /**
     * 获取产品关联列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<RelativeResult>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<ProductRelative> query) {
        View<Page<RelativeResult>> view = new View<Page<RelativeResult>>(20000, "获取列表成功");

        ProductRelative productRelative = query.getData();

        Page<RelativeResult> page = new Page<RelativeResult>(query.getPage(), query.getLimit(), productRelativeService.count(productRelative));
        
        List<ProductRelative> productRelativeList = productRelativeService.getProductRelativeList(query.getOffset(), query.getLimit(), productRelative);
        
        List<RelativeResult> relativeBeanList = new ArrayList<RelativeResult>();

        for (ProductRelative productRelativeDb : productRelativeList) {
            RelativeResult relativeBean = new RelativeResult();
            relativeBean = productRelativeService.getProductByRelativeId(productRelativeDb.getId());
            relativeBeanList.add(relativeBean);
        }
        page.setContent(relativeBeanList);
        
        view.setData(page);
        
        return view;
    }

    /**
     * 删除产品关联
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        ProductRelative productRelative = productRelativeService.selectProductRelative(id);
        productRelative.setDelFlag(String.valueOf(1));
        this.productRelativeService.updateProductRelative(productRelative);

        return view;
    }
    
}
